/**
 * @file max7219matrix.cpp
 *
 */
/* Copyright (C) 2020 by Arjan van Vught mailto:info@orangepi-dmx.nl
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>

#include "max7219matrix.h"

static constexpr auto nDigitsCount = 8;

static constexpr uint8_t nCharsBlinkColon[][8] = {
{ 0x3E, 0x7F, 0x71, 0x59, 0x4D, 0x7F, 0x3E, 0x66 }, // 0:
{ 0x40, 0x42, 0x7F, 0x7F, 0x40, 0x40, 0x00, 0x66 }, // 1:
{ 0x62, 0x73, 0x59, 0x49, 0x6F, 0x66, 0x00, 0x66 }, // 2:
{ 0x22, 0x63, 0x49, 0x49, 0x7F, 0x36, 0x00, 0x66 }, // 3:
{ 0x18, 0x1C, 0x16, 0x53, 0x7F, 0x7F, 0x50, 0x66 }, // 4:
{ 0x27, 0x67, 0x45, 0x45, 0x7D, 0x39, 0x00, 0x66 }, // 5:
{ 0x3C, 0x7E, 0x4B, 0x49, 0x79, 0x30, 0x00, 0x66 }, // 6:
{ 0x03, 0x03, 0x71, 0x79, 0x0F, 0x07, 0x00, 0x66 }, // 7:
{ 0x36, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00, 0x66 }, // 8:
{ 0x06, 0x4F, 0x49, 0x69, 0x3F, 0x1E, 0x00, 0x66 }  // 9:
};

static constexpr uint8_t nCharsBlinkSemiColon[][8] = {
{ 0x3E, 0x7F, 0x71, 0x59, 0x4D, 0x7F, 0x3E | 0x80, 0x66 },	// 0;
{ 0x40, 0x42, 0x7F, 0x7F, 0x40, 0x40, 0x00 | 0x80, 0x66 },	// 1;
{ 0x62, 0x73, 0x59, 0x49, 0x6F, 0x66, 0x00 | 0x80, 0x66 },	// 2;
{ 0x22, 0x63, 0x49, 0x49, 0x7F, 0x36, 0x00 | 0x80, 0x66 },	// 3;
{ 0x18, 0x1C, 0x16, 0x53, 0x7F, 0x7F, 0x50 | 0x80, 0x66 },	// 4;
{ 0x27, 0x67, 0x45, 0x45, 0x7D, 0x39, 0x00 | 0x80, 0x66 },	// 5;
{ 0x3C, 0x7E, 0x4B, 0x49, 0x79, 0x30, 0x00 | 0x80, 0x66 },	// 6;
{ 0x03, 0x03, 0x71, 0x79, 0x0F, 0x07, 0x00 | 0x80, 0x66 },	// 7;
{ 0x36, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00 | 0x80, 0x66 },	// 8;
{ 0x06, 0x4F, 0x49, 0x69, 0x3F, 0x1E, 0x00 | 0x80, 0x66 }  	// 9;
};

static constexpr uint8_t nCharsBlinkComma[][8] = {
{ 0x3E, 0x7F, 0x71, 0x59, 0x4D, 0x7F, 0x3E | 0x80, 0x60 }, 	// 0,
{ 0x40, 0x42, 0x7F, 0x7F, 0x40, 0x40, 0x00 | 0x80, 0x60 }, 	// 1,
{ 0x62, 0x73, 0x59, 0x49, 0x6F, 0x66, 0x00 | 0x80, 0x60 }, 	// 2,
{ 0x22, 0x63, 0x49, 0x49, 0x7F, 0x36, 0x00 | 0x80, 0x60 }, 	// 3,
{ 0x18, 0x1C, 0x16, 0x53, 0x7F, 0x7F, 0x50 | 0x80, 0x60 }, 	// 4,
{ 0x27, 0x67, 0x45, 0x45, 0x7D, 0x39, 0x00 | 0x80, 0x60 }, 	// 5,
{ 0x3C, 0x7E, 0x4B, 0x49, 0x79, 0x30, 0x00 | 0x80, 0x60 }, 	// 6,
{ 0x03, 0x03, 0x71, 0x79, 0x0F, 0x07, 0x00 | 0x80, 0x60 }, 	// 7,
{ 0x36, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00 | 0x80, 0x60 }, 	// 8,
{ 0x06, 0x4F, 0x49, 0x69, 0x3F, 0x1E, 0x00 | 0x80, 0x60 }  	// 9,
};

int main(int argc, char **argv) {
	if (getuid() != 0) {
		fprintf(stderr, "Error: Not started with 'root'\n");
		return -1;
	}

	if (bcm2835_init() != 1) {
		fprintf(stderr, "bcm2835_init() failed\n");
		return -2;
	}

	if (bcm2835_spi_begin() != 1) {
		fprintf(stderr, "bcm2835_spi_begin() failed\n");
		return -3;
	}

	auto nPrevSeconds = 60; // Force initial update
	char buffer[nDigitsCount] = { ' ', '0', '0', '0', '0', '0', '0', ' ' };

	Max7219Matrix matrix;
	matrix.Init(nDigitsCount, 0x40);

	for (uint32_t i = 0; i < sizeof(nCharsBlinkColon)/sizeof(nCharsBlinkColon[0]); i++) {
		matrix.UpdateCharacter(i, nCharsBlinkColon[i]);
	}

	for (uint32_t i = 10; i < 10 + sizeof(nCharsBlinkSemiColon)/sizeof(nCharsBlinkSemiColon[0]); i++) {
		matrix.UpdateCharacter(i, nCharsBlinkSemiColon[i - 10]);
	}

	for (uint32_t i = 20; i < 20 + sizeof(nCharsBlinkComma)/sizeof(nCharsBlinkComma[0]); i++) {
		matrix.UpdateCharacter(i, nCharsBlinkComma[i - 20]);
	}

	for (;;) {
		const auto ltime = time(nullptr);
		const auto tm = localtime(&ltime);

		if (tm->tm_sec != nPrevSeconds) {
			nPrevSeconds = tm->tm_sec;

			const auto bEven = !(((tm->tm_sec % 10) & 0x01) == 0x01);
			const auto nAdd = bEven ? 0 : '0';

			buffer[1] = '0' + tm->tm_hour / 10;
			buffer[2] = nAdd + tm->tm_hour % 10;

			buffer[3] = '0' + tm->tm_min / 10;
			buffer[4] = nAdd + tm->tm_min % 10;

			buffer[5] = '0' + tm->tm_sec / 10;
			buffer[6] = 10 + tm->tm_sec % 10;

			matrix.Write(buffer, nDigitsCount);
		}

	}

	return 0;
}

